package net.huashitong.zhApp.service;

import com.sirdc.modules.sys.filter.NodeNewschedulesModel;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.filter.NewProjectsFilter;
import net.huashitong.Project.service.NewProjectsService;
import net.huashitong.node.entity.NodeNewschedules;
import net.huashitong.user.entity.BnpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AppProjectsService {

    @Autowired
    private NewProjectsService newProjectsService;
    @Lazy
    @Autowired
    private AppReportService appReportService;

    public List<NewProjects> queryProjectsPageByUser(BnpUser user, NewProjectsFilter filter) {
        String phone = user.getPhone();
        String branchType = user.getBranchType();
        String role = user.getRole();
        if ("4".equals(branchType) || "2".equals(branchType) || "1".equals(branchType)) {
            //管理员或者审批部门或者督查组
        } else if ("0".equals(branchType) && "经办人".equals(role)) {
            //业主单位经办人
            filter.setContactPhone(phone);
        } else if ("0".equals(branchType) && "责任人".equals(role)) {
            //业主单位责任人
            filter.setOwnerUnitPersonPhone(phone);
        }
        if ("6".equals(branchType)) {
            //分管领导
            filter.setResPhone(phone);
        }
        //设置默认排序
        if (StringUtils.isBlank(filter.getPaging().getSortCol())) {
            filter.getPaging().setSortCol("sysId");
            filter.getPaging().setSortVal("desc");
        }

        List<NewProjects> resList = newProjectsService.getProjectInfo4(filter);
        return resList;
    }


    public List<NewProjects> queryProjectsNoPageByUser(BnpUser user, NewProjectsFilter filter) {
        String phone = user.getPhone();
        String branchType = user.getBranchType();
        String role = user.getRole();
        if ("4".equals(branchType) || "2".equals(branchType) || "1".equals(branchType)) {
            //管理员或者审批部门或者督查组
        } else if ("0".equals(branchType) && "经办人".equals(role)) {
            //业主单位经办人
            filter.setContactPhone(phone);
        } else if ("0".equals(branchType) && "责任人".equals(role)) {
            //业主单位责任人
            filter.setOwnerUnitPersonPhone(phone);
        }
        if ("6".equals(branchType)) {
            //分管领导
            filter.setResPhone(phone);
        }

        List<NewProjects> resList = newProjectsService.getProjectInfoNoPage(filter);
        return resList;
    }

    /**
     * 小节点名称前加大节点的名称（返回小节点）
     *
     * @param nodes
     * @return
     */
    public List<NodeNewschedules> dealWithNodes(List<NodeNewschedules> nodes) {
        Map<String, String> bigNodeMap = new HashMap<>();
        for (NodeNewschedules node : nodes) {
            if ("1".equals(node.getType())) {
                if (!bigNodeMap.containsKey(node.getNodeId())) {
                    bigNodeMap.put(node.getNodeId(), node.getNodeName());
                }
            }
        }
        List<NodeNewschedules> result = new ArrayList<>();
        for (NodeNewschedules node : nodes) {
            if ("2".equals(node.getType())) {
                if (ObjectUtils.isNotBlank(bigNodeMap.get(node.getNodeIds()))) {
                    String nodeName = bigNodeMap.get(node.getNodeIds());
                    String oldName = node.getNodeName();
                    node.setNodeName(nodeName + "-" + oldName);
                    result.add(node);
                }
            }
        }
        return result;
    }

    /**
     * 小节点名称前加大节点的名称（返回所有节点）
     *
     * @param nodes
     * @return
     */
    public List<NodeNewschedulesModel> dealWithNodes2(List<NodeNewschedulesModel> nodes) {
        Map<String, String> bigNodeMap = new HashMap<>();
        for (NodeNewschedulesModel node : nodes) {
            if ("1".equals(node.getType())) {
                if (!bigNodeMap.containsKey(node.getNodeId())) {
                    bigNodeMap.put(node.getNodeId(), node.getNodeName());
                }
            }
        }
        for (NodeNewschedulesModel node : nodes) {
            if ("2".equals(node.getType())) {
                if (ObjectUtils.isNotBlank(bigNodeMap.get(node.getNodeIds()))) {
                    String nodeName = bigNodeMap.get(node.getNodeIds());
                    String oldName = node.getNodeName();
                    node.setNodeName(nodeName + "-" + oldName);
                }
            }
        }
        return nodes;
    }


    public Map<String, Object> queryProjectDetailByProject(NewProjects project) throws ParseException {
        Map<String, Object> resMap = new HashMap<>();
        String status = project.getStatus();
        String state;
        switch (status) {
            case "1":
                state = "预备";
                break;
            case "2":
                state = "预备";
                break;
            case "3":
                state = "前期节点进行中";
                break;
            case "4":
                state = "已开工";
                break;
            case "6":
                state = "暂停";
                break;
            default:
                state = "-";
                break;
        }

        String aa1 = project.getName() + "项目，位于" + project.getAddress()
                + "，计划投资总额" + project.getMoney() + "万元。";
        String aa2 = "分管县领导：" + project.getResUser();
        String aa3 = "责任单位：" + project.getResDept();
        String aa4 = "项目目前处于" + state + "阶段";

        if ("3".equals(status)) {
            List<NodeNewschedulesModel> list = appReportService.getNodeDetailByProjectId(project.getSysId(), null);
            Map<String, NodeNewschedulesModel> bigNodeMap = new HashMap<>();
            for (NodeNewschedulesModel model : list) {
                //大节点
                if ("1".equals(model.getType())
                        && "1".equals(model.getState())
                        && !(bigNodeMap.containsKey(model.getNodeId()))) {
                    bigNodeMap.put(model.getNodeId(), model);
                }
            }
            //已完成大节点
            StringBuffer finishBigNodes = new StringBuffer();
            //未完成大节点
            StringBuffer noFinishBigNodes = new StringBuffer();
            int noFinishTime = 0;
            int noFinishCount = 0;
            int doingCount = 0;
            int finishCount = 0;
            int planTime = 0;

            //已完成节点集合
            List<String> finishNodesList = new ArrayList<>();
            //未完成节点集合
            List<String> noFinishNodesList = new ArrayList<>();
            //进行中节点集合
            List<String> doingNodesList = new ArrayList<>();

            //进行中的小节点
            StringBuffer doingNodeIds = new StringBuffer();
            List<String> planBeginTimeList = new ArrayList<>();
            List<String> planEndTimeList = new ArrayList<>();
            for (NodeNewschedulesModel model : list) {
                //小节点
                if ("2".equals(model.getType())
                        && "1".equals(model.getState())
                        && "1".equals(model.getStatus())
                        && bigNodeMap.containsKey(model.getNodeIds())) {
                    planBeginTimeList.add(model.getPlanBeginTime());
                    planEndTimeList.add(model.getPlanEndTime());
                    NodeNewschedulesModel m = bigNodeMap.get(model.getNodeIds());
                    doingNodeIds.append(model.getNodeId() + " " + model.getNodeName() + ";");
                    planTime = (StringUtils.isNotBlank(model.getPlanTime()) ? Integer.parseInt(model.getPlanTime()) : 0)
                            + planTime;
                    doingCount++;
                    doingNodesList.add(model.getNodeId() + " " + model.getNodeName());
                }
                //已完成大节点
                if ("1".equals(model.getType())
                        && "1".equals(model.getState())
                        && "2".equals(model.getStatus())) {
                    finishBigNodes.append(model.getNodeId() + model.getNodeName() + ";");
                    finishNodesList.add(model.getNodeId() + model.getNodeName());
                    finishCount++;
                }

                //未完成大节点
                if ("1".equals(model.getType())
                        && "1".equals(model.getState())
                        && ("0".equals(model.getStatus()) || "1".equals(model.getStatus()))) {
                    noFinishCount++;
                    noFinishBigNodes.append(model.getNodeId() + model.getNodeName() + ";");
                    noFinishTime = (StringUtils.isNotBlank(model.getPlanTime()) ? Integer.parseInt(model.getPlanTime()) : 0)
                            + noFinishTime;
                    noFinishNodesList.add(model.getNodeId() + model.getNodeName());
                }
            }
            if (StringUtils.isNotBlank(finishBigNodes.toString())) {
//                String aa5 = "已完成节点：" + finishBigNodes.toString();
                String aa5 = "已完成节点：" + finishCount + "个。";
                resMap.put("aa5", aa5);
            }
            if (StringUtils.isNotBlank(noFinishBigNodes.toString())) {
//                String aa6 = "未完成节点有"+ noFinishCount +"个：" + noFinishBigNodes.toString() +"，计划用时："+ noFinishTime +"天";
                String aa6 = "未完成节点：" + noFinishCount + "个。";
                resMap.put("aa6", aa6);
            }
            if (StringUtils.isNotBlank(doingNodeIds.toString())) {
//                String aa7 = "进行中的节点：" + doingNodeIds.toString() +"，计划用时："+ planTime +"天";
                String planTimeDetail = "";
                if (ObjectUtils.isNotBlank(planBeginTimeList)) {
                    planTimeDetail = "计划开始时间：" + planBeginTimeList.get(0) + "，";
                }
                if (ObjectUtils.isNotBlank(planEndTimeList)) {
                    planTimeDetail = planTimeDetail + "计划结束时间：" + planEndTimeList.get(0) + "，";
                }
                String aa7 = "进行中的节点：" + doingNodeIds.toString() + "总共" + doingCount + "个，" + planTimeDetail + "计划用时：" + planTime + "天。";
                resMap.put("aa7", aa7);
            }
            Map<String,Object> map = new HashMap<>();
            map.put("finishNodesList",finishNodesList);
            map.put("noFinishNodesList",noFinishNodesList);
            map.put("doingNodesList",doingNodesList);
            resMap.put("data1",map);
        }
        resMap.put("aa1", aa1);
        resMap.put("aa2", aa2);
        resMap.put("aa3", aa3);
        resMap.put("aa4", aa4);
        resMap.put("aa8", "项目节点开始时间："+project.getPlanBeginDate());
        resMap.put("aa9", "项目节点结束时间："+project.getStartTime());
        return resMap;
    }

}
